home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH02 / TRUTHTBL / TRUTHU.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1995-12-16  |  10.8 KB  |  397 lines

  1. (*****************************************************************************)
  2. (*                                                                           *)
  3. (* Truth                                                                     *)
  4. (*                                                                           *)
  5. (* 10/25/95                                                                  *)
  6. (* Randall L. Hyde                                                           *)
  7. (* Copyright 1995, All Rights Reserved Unless Otherwise Noted                *)
  8. (*                                                                           *)
  9. (* This program converts truth tables into logic equations.                  *)
  10. (*                                                                           *)
  11. (* Runs under Windows 3.1, Windows 95, and Windows NT.                       *)
  12. (* Source Code: Borland Delphi (object Pascal).                              *)
  13. (*                                                                           *)
  14. (*****************************************************************************)
  15.  
  16.  
  17. unit Truthu;
  18.  
  19. interface
  20.  
  21. uses
  22.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  23.   Forms, Dialogs, StdCtrls, ExtCtrls, Converts, Aboutp;
  24.  
  25. type
  26.  
  27.   { Class definition for the conversion form }
  28.  
  29.   TTruthTbl = class(TForm)
  30.  
  31.     tt00: TPanel;    {These panels correspond to the entries in the    }
  32.     tt01: TPanel;    {truth table, e.g., tt[0..3][0..3]        }
  33.     tt02: TPanel;
  34.     tt03: TPanel;
  35.     tt10: TPanel;
  36.     tt11: TPanel;
  37.     tt12: TPanel;
  38.     tt13: TPanel;
  39.     tt20: TPanel;
  40.     tt21: TPanel;
  41.     tt22: TPanel;
  42.     tt23: TPanel;
  43.     tt30: TPanel;
  44.     tt31: TPanel;
  45.     tt32: TPanel;
  46.     tt33: TPanel;
  47.  
  48.     Line1: TPanel;
  49.     Line2: TPanel;
  50.  
  51.     dc00: TLabel;
  52.     dc01: TLabel;
  53.     dc10: TLabel;
  54.     dc11: TLabel;
  55.     ba00: TLabel;
  56.     ba01: TLabel;
  57.     ba10: TLabel;
  58.     ba11: TLabel;
  59.  
  60.     Eqn1: TLabel;    {This is where we store the logic eqn string    }
  61.     Eqn2: TLabel;
  62.  
  63.     TwoVarsBtn: TButton;
  64.     ThreeVarsBtn: TButton;
  65.     FourVarsBtn: TButton;
  66.     ExitBtn: TButton;
  67.     AboutBtn: TButton;
  68.     PrintBtn: TButton;
  69.     PrintDialog: TPrintDialog;
  70.  
  71.     procedure TwoVarsBtnClick(Sender: TObject);
  72.     procedure ThreeVarsBtnClick(Sender: TObject);
  73.     procedure FourVarsBtnClick(Sender: TObject);
  74.     procedure tClick(Sender: TObject);
  75.     procedure ExitBtnClick(Sender: TObject);
  76.     procedure AboutBtnClick(Sender: TObject);
  77.     procedure PrintBtnClick(Sender: TObject);
  78.  
  79.  
  80.   end;
  81.  
  82.  
  83.  
  84.  
  85. {Instance variable for this form }
  86.  
  87. var
  88.   TruthTbl: TTruthTbl;
  89.  
  90.  
  91.  
  92.  
  93. implementation
  94.  
  95.  
  96. {$R *.DFM}
  97.  
  98.  
  99.  
  100. (* ComputeEqn-    Computes the logic equation string from the current    *)
  101. (*         truth table entries.                    *)
  102.  
  103. procedure ComputeEqn;
  104.  
  105.     { ApndStr-    item contains '0' or '1' -- the character in the}
  106.     {        current truth table cell.  theStr is a string    }
  107.     {        of characters to append to the equation if item    }
  108.     {        is equal to '1'.                }
  109.  
  110.     procedure ApndStr(item:char; const theStr:string);
  111.     begin
  112.  
  113.       with TruthTbl do begin
  114.  
  115.         { To make everything fit on our form, we have to break    }
  116.         { the equation up into two lines.  If the first line    }
  117.         { hits 66 characters, append the characters to the end    }
  118.         { of the second string.                    }
  119.  
  120.         if (length(eqn1.Caption) < 66) then begin
  121.  
  122.            { If we are appending to the end of EQN1, we have to    }
  123.            { check to see if the string's length is zero.  If    }
  124.            { not, then we need to stick ' + ' between the    }
  125.            { existing string and the string we are appending.    }
  126.            { If the string length is zero, this is the first    }
  127.            { minterm so we don't prepend the ' + '.        }
  128.  
  129.            if (item = '1') then
  130.             if (length(eqn1.Caption) = 0) then
  131.                      eqn1.Caption := theStr
  132.             else eqn1.Caption :=  eqn1.Caption + ' + ' + theStr;
  133.         end
  134.         else if (item = '1') then
  135.             eqn2.Caption :=  eqn2.Caption + ' + ' + theStr;
  136.  
  137.       end;
  138.  
  139.     end;
  140.  
  141.  
  142. begin
  143.  
  144.     with TruthTbl do begin
  145.  
  146.  
  147.         eqn1.Caption := '';
  148.         eqn2.Caption := '';
  149.  
  150.     { Determine if two variable truth table.  tt12    }
  151.         { will only be visible if we've got a three or    }
  152.         { four variable truth table.            }
  153.  
  154.         if (not tt12.Visible) then begin
  155.  
  156.             { Test the 2x2 square in the upper left    }
  157.             { hand corner of the truth table and build    }
  158.             { the logic equation from the values in    }
  159.             { these squares.                }
  160.  
  161.             ApndStr(tt00.Caption[1],'B''A''');
  162.             ApndStr(tt01.Caption[1],'B''A');
  163.             ApndStr(tt10.Caption[1], 'BA''');
  164.             ApndStr(tt11.Caption[1], 'BA');
  165.  
  166.  
  167.         end
  168.         else begin {We've got three or four variables here }
  169.  
  170.             { See if three or four variable truth table    }
  171.             { tt20 will only be visible if we have a    }
  172.             { four variable truth table.        }
  173.  
  174.             if (not tt20.Visible) then begin
  175.  
  176.                 { Build the logic equation from the top    }
  177.                 { eight squares in the truth table.    }
  178.  
  179.                 ApndStr(tt00.Caption[1],'C''B''A''');
  180.                 ApndStr(tt01.Caption[1],'C''B''A');
  181.                 ApndStr(tt02.Caption[1], 'C''BA''');
  182.                 ApndStr(tt03.Caption[1], 'C''BA');
  183.  
  184.                 ApndStr(tt10.Caption[1],'CB''A''');
  185.                 ApndStr(tt11.Caption[1],'CB''A');
  186.                 ApndStr(tt12.Caption[1], 'CBA''');
  187.                 ApndStr(tt13.Caption[1], 'CBA');
  188.  
  189.             end
  190.             else begin {We've got a four-variable truth table    }
  191.  
  192.                 { Build the logic equation from all the squares    }
  193.                 { in the truth table.                }
  194.  
  195.                 ApndStr(tt00.Caption[1],'D''C''B''A''');
  196.                 ApndStr(tt01.Caption[1],'D''C''B''A');
  197.                 ApndStr(tt02.Caption[1], 'D''C''BA''');
  198.                 ApndStr(tt03.Caption[1], 'D''C''BA');
  199.  
  200.                 ApndStr(tt10.Caption[1],'D''CB''A''');
  201.                 ApndStr(tt11.Caption[1],'D''CB''A');
  202.                 ApndStr(tt12.Caption[1], 'D''CBA''');
  203.                 ApndStr(tt13.Caption[1], 'D''CBA');
  204.  
  205.                 ApndStr(tt20.Caption[1],'DC''B''A''');
  206.                 ApndStr(tt21.Caption[1],'DC''B''A');
  207.                 ApndStr(tt22.Caption[1], 'DC''BA''');
  208.                 ApndStr(tt23.Caption[1], 'DC''BA');
  209.  
  210.                 ApndStr(tt30.Caption[1],'DCB''A''');
  211.                 ApndStr(tt31.Caption[1],'DCB''A');
  212.                 ApndStr(tt32.Caption[1], 'DCBA''');
  213.                 ApndStr(tt33.Caption[1], 'DCBA');
  214.  
  215.             end;
  216.  
  217.         end;
  218.  
  219.         { If after all the above the string is empty, then we've got a    }
  220.         { truth table that contains all zeros.  Handle that special    }
  221.         { case down here.                        }
  222.  
  223.         if (length(eqn1.Caption) = 0) then
  224.            eqn1.Caption := '0';
  225.         Eqn1.Caption := 'F= ' + Eqn1.Caption;
  226.  
  227.     end;
  228.  
  229. end;
  230.  
  231.  
  232. { If the user hits the "Two Variables" button, turn off all the squares    }
  233. { in the truth table except the upper left 2x2 block.  Change the labels}
  234. { on the truth table as appropriate.                    }
  235.  
  236. procedure TTruthTbl.TwoVarsBtnClick(Sender: TObject);
  237. begin
  238.  
  239.     ba00.Caption := 'A''';
  240.         ba01.Caption := 'A';
  241.         ba10.Caption := '';
  242.         ba11.Caption := '';
  243.  
  244.         dc00.Caption := 'B''';
  245.         dc01.Caption := 'B';
  246.         dc10.Caption := '';
  247.         dc11.Caption := '';;
  248.  
  249.         tt02.Visible := false;
  250.         tt03.Visible := false;
  251.  
  252.         tt12.Visible := false;
  253.         tt13.Visible := false;
  254.  
  255.         tt20.Visible := false;
  256.         tt21.Visible := false;
  257.         tt22.Visible := false;
  258.         tt23.Visible := false;
  259.  
  260.         tt30.Visible := false;
  261.         tt31.Visible := false;
  262.         tt32.Visible := false;
  263.         tt33.Visible := false;
  264.  
  265.         Eqn2.Visible := false;
  266.  
  267.         tt02.Caption := '0';
  268.         tt03.Caption := '0';
  269.         tt12.Caption := '0';
  270.         tt13.Caption := '0';
  271.         tt20.Caption := '0';
  272.         tt21.Caption := '0';
  273.         tt22.Caption := '0';
  274.         tt23.Caption := '0';
  275.         tt30.Caption := '0';
  276.         tt31.Caption := '0';
  277.         tt32.Caption := '0';
  278.         tt33.Caption := '0';
  279.  
  280.         ComputeEqn;
  281.  
  282. end;
  283.  
  284. { If the user presses the "Three Variables" button, turn off the bottom    }
  285. { eight squares and adjust the labels as appropriate.  This code also    }
  286. { turns on any squares turned off by TwoVarsBtnClick above.        }
  287.  
  288. procedure TTruthTbl.ThreeVarsBtnClick(Sender: TObject);
  289. begin
  290.     ba00.Caption := 'B''A''';
  291.         ba01.Caption := 'B''A';
  292.         ba10.Caption := 'BA''';
  293.         ba11.Caption := 'BA';
  294.  
  295.         dc00.Caption := 'C''';
  296.         dc01.Caption := 'C';
  297.         dc10.Caption := '';
  298.         dc11.Caption := '';;
  299.  
  300.         tt02.Visible := true;
  301.         tt03.Visible := true;
  302.  
  303.         tt12.Visible := true;
  304.         tt13.Visible := true;
  305.  
  306.         tt20.Visible := false;
  307.         tt21.Visible := false;
  308.         tt22.Visible := false;
  309.         tt23.Visible := false;
  310.  
  311.         tt30.Visible := false;
  312.         tt31.Visible := false;
  313.         tt32.Visible := false;
  314.         tt33.Visible := false;
  315.  
  316.         Eqn2.Visible := true;
  317.  
  318.         tt20.Caption := '0';
  319.         tt21.Caption := '0';
  320.         tt22.Caption := '0';
  321.         tt23.Caption := '0';
  322.         tt30.Caption := '0';
  323.         tt31.Caption := '0';
  324.         tt32.Caption := '0';
  325.         tt33.Caption := '0';
  326.         ComputeEqn;
  327.  
  328. end;
  329.  
  330. { This procedure turns on all the squares when the user presses    }
  331. { the "Four Variables" button.                    }
  332.  
  333. procedure TTruthTbl.FourVarsBtnClick(Sender: TObject);
  334. begin
  335.     ba00.Caption := 'B''A''';
  336.         ba01.Caption := 'B''A';
  337.         ba10.Caption := 'BA''';
  338.         ba11.Caption := 'BA';
  339.  
  340.         dc00.Caption := 'D''C''';
  341.         dc01.Caption := 'D''C';
  342.         dc10.Caption := 'DC''';
  343.         dc11.Caption := 'DC';;
  344.  
  345.         tt02.Visible := true;
  346.         tt03.Visible := true;
  347.  
  348.         tt12.Visible := true;
  349.         tt13.Visible := true;
  350.  
  351.         tt20.Visible := true;
  352.         tt21.Visible := true;
  353.         tt22.Visible := true;
  354.         tt23.Visible := true;
  355.  
  356.         tt30.Visible := true;
  357.         tt31.Visible := true;
  358.         tt32.Visible := true;
  359.         tt33.Visible := true;
  360.  
  361.         Eqn2.Visible := true;
  362.         ComputeEqn;
  363. end;
  364.  
  365. { Whenever the user clicks on one of the squares in the truth table,    }
  366. { this procedure toggles its label between '0' <-> '1'.            }
  367.  
  368. procedure TTruthTbl.tClick(Sender: TObject);
  369. var t:TPanel;
  370. begin
  371.         t:=TPanel(Sender);
  372.     t.Caption := chr(ord(t.Caption[1]) xor 1);
  373.         ComputeEqn;
  374.  
  375. end;
  376.  
  377.  
  378. procedure TTruthTbl.ExitBtnClick(Sender: TObject);
  379. begin
  380.     Halt;
  381. end;
  382.  
  383. procedure TTruthTbl.AboutBtnClick(Sender: TObject);
  384. begin
  385.  
  386.     AboutBox.Show;
  387.  
  388. end;
  389.  
  390. procedure TTruthTbl.PrintBtnClick(Sender: TObject);
  391. begin
  392.     if (PrintDialog.Execute) then
  393.         TruthTbl.Print;
  394. end;
  395.  
  396. end.
  397.